package com.asion.vmovie.fragment;

import android.graphics.Color;
import android.os.Bundle;
import android.os.Handler;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.DefaultItemAnimator;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;

import com.asion.vmovie.R;
import com.asion.vmovie.adapters.PingDaoRecyclerAdapter;
import com.asion.vmovie.beans.ChannalsBean;
import com.asion.vmovie.ui.MVPContract.PingDapContract;
import com.asion.vmovie.ui.MVPModel.PingDaoModel;
import com.asion.vmovie.ui.MVPPresenter.PingDaoPresenter;

import org.greenrobot.eventbus.EventBus;

import butterknife.BindView;

/**
 * Created by asion on 16-10-17.
 */

public class PingDaoFragment extends BaseFragment<PingDaoPresenter,PingDaoModel> implements SwipeRefreshLayout.OnRefreshListener,PingDapContract.View {

    @BindView(R.id.recycler)
    RecyclerView recycler;
    @BindView(R.id.refreshlayout)
    SwipeRefreshLayout refreshlayout;
    private PingDaoRecyclerAdapter pingDaoRecyclerAdapter;

    public static PingDaoFragment newInstance(){
        return new PingDaoFragment();
    }
    @Override
    protected int getLayoutResource() {
        return R.layout.recycler_fragment;
    }

    @Override
    public void initPresenter() {
        mPresenter.setVM(this,mModel);
        mPresenter.getPingDaoData();
    }

    @Override
    protected void initView() {
        refreshlayout.setColorSchemeColors(Color.BLUE,Color.CYAN,Color.GRAY);
        pingDaoRecyclerAdapter = new PingDaoRecyclerAdapter(getContext());
        recycler.setItemAnimator(new DefaultItemAnimator());
        recycler.setLayoutManager(new LinearLayoutManager(getContext()));
        recycler.setAdapter(pingDaoRecyclerAdapter);
        refreshlayout.setOnRefreshListener(this);
        pingDaoRecyclerAdapter.setOnItemClickListener(dataBean -> {
            Bundle bundle = new Bundle();
            bundle.putString("title",dataBean.getCatename());
            bundle.putString("cateid",dataBean.getCateid());
            EventBus.getDefault().post(bundle);
        });
        recycler.addOnScrollListener(new RecyclerView.OnScrollListener() {
            @Override
            public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
                if(dy > 0){
                    EventBus.getDefault().post("hide");
                }else{
                    EventBus.getDefault().post("show");
                }
            }
        });
    }

    @Override
    public void onRefresh() {
        pingDaoRecyclerAdapter.removeAll();
        mPresenter.getPingDaoData();
        new Handler().postDelayed(()->refreshlayout.setRefreshing(false),3000);
    }

    @Override
    public void showDataSuccess(ChannalsBean channalsBean) {
        pingDaoRecyclerAdapter.removeAll();
        pingDaoRecyclerAdapter.addAll(channalsBean.getData());
    }

    @Override
    public void showDataError(Throwable throwable) {

    }
}
